import os
import re
import logging
import logging.handlers


class LogHandler:
    def __init__(self, name):
        file_path = os.path.dirname(os.path.dirname(__file__)) + "/log/" + name
        print(file_path)
        file_handler = logging.handlers.TimedRotatingFileHandler(filename=file_path, when="MIDNIGHT", interval=1, backupCount=7)
        file_handler.suffix = "%Y-%m-%d.log"
        file_handler.extMatch = re.compile(r"^\d{4}-\d{2}-\d{2}.log$")
        file_handler.setLevel(logging.INFO)
        style = '%(asctime)s - %(levelname)s - %(module)s - %(funcName)s - lineno:%(lineno)s - %(message)s'
        formatter = logging.Formatter(style)
        file_handler.setFormatter(formatter)

        self.logger = logging.getLogger(name)
        self.logger.setLevel(logging.INFO)
        self.logger.addHandler(file_handler)


logger = LogHandler("run").logger

if __name__ == '__main__':
    logger = LogHandler("test").logger
    logger.info("this is test.............")
    logger.debug('debug')
    logger.info('info')
    logger.warning('警告')
    logger.error('报错')

